// Camera.cpp: implementation of the CLiveCamera class.
//
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)

#include "stdafx.h"
#include "RMSTranslator.h"
#include "../RMSServer/RMSBaseServer.h"
#include "Camera.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLiveCamera::CLiveCamera(CBaseServer *p)

{
	pBaseServer=p;
	m_waveStream.pBaseServer=p;
	m_videoStream.pBaseServer=p;

	m_bActive = FALSE;
	m_nCameraID = 0;
}

CLiveCamera::~CLiveCamera()
{

}

BOOL CLiveCamera::Init(int nCameraID, SOCKADDR_IN saVideo)
{
	CString ip;
	ip=pBaseServer->GetProperIPForCamera(inet_ntoa(saVideo.sin_addr));
	SetIP(ip);
	m_nCameraID = nCameraID;
	m_saVideo = saVideo;

	m_videoStream.Init(saVideo, this);

	m_bActive = TRUE;
	lastReceivedVideoTime=
	lastReceivedAudioTime=0;
	//		timeGetTime(); // 
	return TRUE;
}

void CLiveCamera::EnableNoVideo(BOOL bEnable)
{
	m_videoStream.EnableNoVideo(bEnable);
}

BOOL CLiveCamera::Stop()
{
	m_bActive = FALSE;

	m_videoStream.Stop();
	m_waveStream.Stop();

	return TRUE;
}

int CLiveCamera::GetID() const
{
	return m_nCameraID;
}

BOOL CLiveCamera::IsActive() const
{
	return m_bActive;
}

BOOL CLiveCamera::Update()
{
	if(m_bActive)
	{
		if (m_videoStream.m_pOwner==0 ||
			m_waveStream.m_pOwner ==0)
			return 1; // not Inited yet
		// check status
		if (!m_videoStream.IsActive())  m_bActive = FALSE;
		if (!m_waveStream.IsActive())   m_bActive = FALSE;

		// if still alive, check for the last received packet
		if(m_bActive){
			DWORD cTime=timeGetTime();
			DWORD lastReceivedTime=cTime;
			int max_time_out=10;

			if (lastReceivedVideoTime) {
					lastReceivedTime=lastReceivedVideoTime;
			}
			if (lastReceivedAudioTime && lastReceivedAudioTime<lastReceivedVideoTime) {
					max_time_out = 5;// set 2 seconds time-out for audio
					lastReceivedTime=lastReceivedAudioTime;
			}

			if (cTime>lastReceivedTime) cTime-=lastReceivedTime;
			else cTime=lastReceivedTime-cTime;

			// 15 seconds time out- restart camera conectio if needed
			if (cTime>max_time_out*1000) {
				lastReceivedVideoTime=0;
				//lastReceivedAudioTime=0;
				m_videoStream.Stop();
				m_waveStream.Stop();
				m_bActive = FALSE;
			}
		}

		// reconnect if needed
		if (m_bActive==FALSE) {
			Sleep(100);
			m_videoStream.Restart();
			Sleep(100);
			if (lastReceivedAudioTime) lastReceivedAudioTime=timeGetTime();
			m_waveStream.Restart();
			Sleep(100);
			m_bActive = TRUE;
		}
	}
	return m_bActive;
}

BOOL CLiveCamera::SetIP(const char* szIP)
{
	if(IsActive())
	{
		Stop();
		strcpy(m_szIP, szIP);
		Init(m_nCameraID, m_saVideo); 
	}
	else
	{
		strcpy(m_szIP, szIP);
	}
	return TRUE;
}

char* CLiveCamera::GetIP()
{
	return m_szIP;
}